<?php

namespace app\admin\model;

use think\facade\Config;
use think\facade\Request;
use think\Model;

class Goods extends Model{
    public function getRecommendedAttr($value,$data){
        if (Config::has('param.param_recommended')){
            $config1 = Config::get('param.param_recommended');
            return $config1[$data['recommended']];
        }
    }
    public function getIfShowAttr($value){
        if (Config::has('param.param_if_show')){
            $config = Config::get('param.param_if_show');
            return $config[$value];
        }
    }
    public function getIfOpenAttr($value){
        if (Config::has('param.param_if_open')){
            $config2 = Config::get('param.param_if_open');
            return $config2[$value];
        }
    }
    public function getAddTimeAttr($value){
        return date('Y-m-d H:i:s',$value);
    }

    public function searchRecommendedAttr($query,$value){
        if(isset($value) && '' !== $value){
            $query->where("ecm_goods.recommended",$value);
        }
    }
    public function searchIfShowAttr($query,$value){
        if(isset($value) && '' !== $value){
            $query->where("ecm_goods.if_show",$value);
        }
    }
    public function searchIfOpenAttr($query,$value){
        if(isset($value) && '' !== $value){
            $query->where("ecm_goods.if_open",$value);
        }
    }
    public function searchKeywordsAttr($query,$value,$data){
        if(''!==$value){
            $query->whereLike('goods_name','%'.$value.'%');
        }

    }
    public function searchMaxtimeAttr($query,$value){
        if(!empty($value)){
            $query->whereTime('add_time', '>=', $value);
        }
    }
    public function searchMintimeAttr($query,$value){
        if(!empty($value)){
            $query->whereTime('add_time', '<=', $value);
        }
    }
    public function searchCateIdAttr($query,$value){
        if(isset($value) && '' !== $value){
            $query->where("ecm_goods.cate_id",$value);
        }
    }
    public function searchMaxQianAttr($query,$value){
        if(!empty($value)){
            $query->where('price','>=', $value);
        }
    }
    public function searchMinQianAttr($query,$value){
        if(!empty($value)){
            $query->where('price','<=', $value);
        }
    }


    public static function golist($data=[],$query=[],$page=true){
        $model = self::field("ecm_goods.*,ecm_goods.if_open,ecm_brand.brand_name,ecm_gcategory.cate_name")
            ->withSearch(array_keys($data),$data)
            ->join("ecm_brand",'ecm_goods.brand=ecm_brand.brand_id',"LEFT")
            ->join("ecm_gcategory","ecm_goods.cate_id=ecm_gcategory.cate_id","LEFT");
//        if(isset($data['recommended']) && '' !== $data['recommended']){
//            $model->where("ecm_goods.recommended",$data['recommended']);
//        }
//        if(isset($data['if_show']) && '' !== $data['if_show']){
//            $model->where("ecm_goods.if_show",$data['if_show']);
//        }
//        if(isset($data['if_open']) && '' !== $data['if_open']){
//            $model->where("ecm_goods.if_open",$data['if_open']);
//        }
//        if(isset($data['keywords']) && '' !== $data['keywords']){
//            $model->whereLike('goods_name',"%{$data['keywords']}%");
//        }
//        if(isset($data['maxtime']) && '' !== $data['maxtime']){
//            $model->whereTime('add_time','>=',$data['maxtime']);
//        }
//        if(isset($data['mintime']) && '' !== $data['mintime']){
//            $model->whereTime('add_time','<=',$data['mintime']);
//        }
//        if(isset($data['cate_id']) && '' !== $data['cate_id']){
//            $model->where("ecm_goods.cate_id",$data['cate_id']);
//        }
//        if(isset($data['maxqian']) && '' !== $data['maxqian']){
//            $model->where('price','>=',$data['maxqian']);
//        }
//        if(isset($data['minqian']) && '' !== $data['minqian']){
//            $model->where('price','<=',$data['minqian']);
//        }
        if($page){
            return $model->paginate([
              'list_rows'=>10,
              'query'=>$query
            ])->each(function ($v,$k){
                $v['goods_name'] = str_replace(Request::get('keywords'),"<span style='color: red;font-weight: bolder;'>".Request::get('keywords')."</span>",$v['goods_name']);
                return $v;
            });
        }
        return $model->select();

    }
}